import { defineComponent, PropType } from 'vue';

// schema的类型
export enum SchemaType {
  'number' = 'number',
  'string' = 'string',
  'integer' = 'integer',
  'boolean' = 'boolean',

  'object' = 'object', // object 里边也会包含数组
  'array' = 'array',
}

type SchemaRef = {
  $ref: string; // 记录这 rootschema 上引用的路径
};

// 定义Schema的属性
export interface Schema {
  type?: SchemaType | string;
  const?: any;
  format?: string;

  title?: string;
  default?: any;
  // 对象的属性
  properties?: {
    // schemaForm 的属性字段， 值又是一个 schema
    [key: string]: Schema;
  };
  // 数组每一项的配置
  items?: Schema | Schema[] | SchemaRef;
  uniqueItems?: any;
  dependencies?: {
    [key: string]: string[] | Schema | SchemaRef;
  };
  oneOf?: Schema[];
  anyOf?: Schema[];
  allOf?: Schema[];
  // TODO: uiSchema
  // vjsf?: VueJsonSchemaConfig
  required?: string[]; // 必填的 properties 字段
  enum?: any[];
  enumNames?: any[];
  enumKeyValue?: any[];
  additionalProperties?: any;
  additionalItems?: Schema;

  minLength?: number;
  maxLength?: number;
  minimun?: number;
  maximum?: number;
  multipleOf?: number;
  exclusiveMaximum?: number;
  exclusiveMinimum?: number;
}

// 定义 Schema field组件接受的类型
export const SchemaFormPropsDefine = {
  schema: {
    type: Object as PropType<Schema>,
    required: true,
  },
  value: {
    required: true,
  },
  onChange: {
    type: Function as PropType<(v: any) => void>,
    required: true,
  },
  // 原始的根 schema
  rootSchema: {
    type: Object as PropType<Schema>,
    required: true,
  },
} as const;

// 定义 uiSchema 和 schema 的结合体
export interface superSchema {
  schema: Schema;
  uiSchema: any;
  default: any;
}

export const TypeHelperComponent = defineComponent({
  props: SchemaFormPropsDefine,
});

// 自定义一个组件的类型
export type CommonFieldType = typeof TypeHelperComponent;
